/**
 * 
 */
package com.xinghou.platform.dao.main.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.main.UserDAO;
import com.xinghou.platform.model.main.User;
import com.xinghou.platform.server.mq.MqMessage;

/**
 * @author wxtdy
 *
 */
@Repository
public class UserDAOImpl  {/*
	
	@Resource
	private JdbcTemplate jdbcTemplate;

	@Resource
	private MqMessage mqMessage;

	*//* (non-Javadoc)
	 * @see com.xinghou.platform.dao.main.UserDAO#getUserById(java.lang.Integer)
	 *//*

	public User getUserById(Integer userId) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "select * from user where User_id = ?";
		User user = jdbcTemplate.queryForObject(sql, new Object[] { userId }, new UserMapper());
		CustomerContextHolder.clearCustomerType();
		return user;
	}
	
	public User getUserBySecondAccount(String type,String account) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "select * from user where ";
		if(type.equals("QQ")){
			sql = sql +"QQ_id =?";
		}else if(type.equals("WX")){
			sql = sql +"WX_id =?";
		}else{
			sql = sql +"WB_id =?";
		}
		User user = jdbcTemplate.queryForObject(sql, new Object[] { account }, new UserMapper());
		CustomerContextHolder.clearCustomerType();
		return user;
	}

	*//* (non-Javadoc)
	 * @see com.xinghou.platform.dao.main.UserDAO#updateUser(com.xinghou.platform.model.main.User)
	 *//*

	public int updateUser(User user) throws DataAccessException {
		StringBuffer strSQL = new StringBuffer("UPDATE user set Password = ?,")
				.append("Phone_number = ?, Sex = ?, Os_type = ?, User_type = ?, Head_status = ?,")
				.append("Province_code = ?, Province_name = ?, City_code =?, City_name =?, Status = ?, Create_time =?,Update_time =?, Reg_status =?, Nick_name =?, Birthday =?, Signature =?, Head_id =?, QQ_id =?, QQ_nickname =?, WB_id =?, WB_nickname =?, WX_id =?, WX_nickname =?, NAME =?, Store_id =?,QQ_state=?, WX_state=?, WB_state=?,job_start=?,job_time=?,style=? where User_id = ?");
		
		Object[] params = {user.getPassword(), 
				user.getPhoneNumber(), user.getSex(), user.getOsType(), user.getUserType(), user.getHeadStatus(), user.getProvinceCode(), 
				user.getProvinceName(), user.getCityCode(), user.getCityName(),  user.getStatus(), user.getCreateTime(),user.getUpdateTime(),user.getRegStatus(),
        		user.getNickName(),user.getBirthday(),user.getSignature(),user.getHeadId(),user.getQqId(),
        		user.getQqNickname(),user.getWbId(),user.getWbNickname(),user.getWxId(),user.getWxNickname(),
        		user.getName(),user.getStoreId(), user.getQqState(),user.getWxState(),user.getWbState(),
        		user.getJobStart(),user.getJobTime(),user.getStyle(),user.getUserId()
		};
		
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret = jdbcTemplate.update(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		if(ret>0){
			mqMessage.sendMessageUser(user.getUserId()+"",true);
		}
		return ret;
	}

	public int saveUser(User user) throws DataAccessException {
		StringBuffer strSQL = new StringBuffer("INSERT INTO user(Password,Phone_number,Sex,Os_type,User_type,Head_status,")
				.append("Province_code,Province_name,City_code,City_name,Status,Create_time,Update_time, Reg_status, Nick_name, Birthday, Signature, Head_id, QQ_id, QQ_nickname, WB_id, WB_nickname, WX_id, WX_nickname, NAME, Store_id,QQ_state, WX_state, WB_state) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
		Object[] params = {user.getPassword(), 
				user.getPhoneNumber(), user.getSex(), user.getOsType(), user.getUserType(), user.getHeadStatus(),
        		user.getProvinceCode(), user.getProvinceName(), user.getCityCode(), user.getCityName(),  
        		user.getStatus(), user.getCreateTime(),user.getUpdateTime(),user.getRegStatus(),
        		user.getNickName(),user.getBirthday(),user.getSignature(),user.getHeadId(),user.getQqId(),
        		user.getQqNickname(),user.getWbId(),user.getWbNickname(),user.getWxId(),user.getWxNickname(),
        		user.getName(),user.getStoreId(),user.getQqState(),user.getWxState(),user.getWbState()
		};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret = jdbcTemplate.save(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}
	

	public User getUserByMobile(String mobile) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "select * from user where Phone_number = ?";
		User user = jdbcTemplate.queryForObject(sql, new Object[] { mobile }, new UserMapper());
		CustomerContextHolder.clearCustomerType();
		return user;
	}

	public List<User> getUserList(Integer frompage) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql="SELECT * FROM USER where STATUS=1 LIMIT ?,10 ";
		List<User> UserList=jdbcTemplate.query(sql,new Object[] {frompage}, new UserMapper());
		return UserList;
	}

	public List<User> getUserList() throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql="SELECT * FROM USER where STATUS=1";
		List<User> UserList=jdbcTemplate.query(sql,new UserMapper());
		return UserList;
	}
	
	public List<User> getUserById(int userid, int pageSize) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT * FROM user WHERE User_id > ? limit ?";
		List<User> users = jdbcTemplate.query(sql,new Object[] { userid, pageSize }, new UserMapper());
		CustomerContextHolder.clearCustomerType();
		return users;
	}
	
	public List<User> getUserByStoreId(int storeid,int fromno,int limit) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT * FROM user WHERE Store_id = ? and User_id>? order by User_id limit ? ";
		List<User> users = jdbcTemplate.query(sql,new Object[] { storeid,fromno,limit }, new UserMapper());
		CustomerContextHolder.clearCustomerType();
		return users;
	}

	public List<User> getUserByDay(int userid, int pageSize) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT * FROM user WHERE User_id > ? and TO_DAYS(Create_time) = TO_DAYS(NOW()) limit ?";
		List<User> users = jdbcTemplate.query(sql,new Object[] { userid, pageSize }, new UserMapper());
		CustomerContextHolder.clearCustomerType();
		return users;
	}

	private static final class UserMapper implements RowMapper<User> {
		public User mapRow(ResultSet rs, int rowNum) throws SQLException {
			User user = new User();
			user.setUserId(rs.getInt("User_id"));
			user.setHeadStatus(rs.getShort("Head_status"));
			user.setOsType(rs.getShort("Os_type"));
			user.setPassword(rs.getString("password"));
			user.setPhoneNumber(rs.getString("Phone_number"));
			user.setStatus(rs.getShort("Status"));
			user.setUserType(rs.getShort("User_type"));
			user.setCityCode(rs.getString("City_code"));
			user.setCityName(rs.getString("City_name"));
			user.setProvinceCode(rs.getString("Province_code"));
			user.setProvinceName(rs.getString("Province_name"));
			user.setSex(rs.getShort("Sex"));
			user.setCreateTime(rs.getTimestamp("Create_time"));
			user.setUpdateTime(rs.getTimestamp("Update_time"));
			user.setRegStatus(rs.getShort("Reg_status"));
			user.setNickName(rs.getString("Nick_name"));
			user.setSignature(rs.getString("Signature"));
			user.setBirthday(rs.getDate("Birthday"));
			user.setHeadId(rs.getInt("Head_id"));
			user.setQqId(rs.getString("QQ_id"));
			user.setQqNickname(rs.getString("QQ_nickname"));
			user.setQqState(rs.getInt("QQ_state"));
			user.setWbId(rs.getString("WB_id"));
			user.setWbNickname(rs.getString("WB_nickname"));
			user.setWbState(rs.getInt("WB_state"));
			user.setWxId(rs.getString("WX_id"));
			user.setWxNickname(rs.getString("WX_nickname"));
			user.setWxState(rs.getInt("WX_state"));
			user.setName(rs.getString("NAME"));
			user.setStoreId(rs.getInt("Store_id"));
			user.setStyle(rs.getString("style"));
			user.setJobStart(rs.getString("job_start"));
			user.setJobTime(rs.getString("job_time"));
			return user;
		}
	}

	@Override
	public List<User> getUserIdBySex(Short sex,String citycode) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql="SELECT * FROM user where Head_id>1 and Sex=? ";
		if(citycode.equals("310100") || citycode.equals("110100") || citycode.equals("120100") || citycode.equals("500100")
			|| citycode.equals("0210") || citycode.equals("0100") || citycode.equals("0220") || citycode.equals("0230")	)	
		{
			sql = sql+"and Province_code=? and Password IS NULL";
		}else{
			sql = sql+"and City_code=? and Password IS NULL";
		}
		// and User_id>11116028 and User_id<11215942
//		if(sex != 1)
//		{
//			sql = "SELECT * FROM user where STATUS=1 and Sex=? and City_code=? and Password IS NULL";
//			// and User_id>10000000 and User_id<10900000
//		}
		List<User> UserList=jdbcTemplate.query(sql,new Object[] {sex,citycode}, new UserMapper());
		return UserList;
	}

	@Override
	public List<User> getUserIdByCitycode(String citycode)
			throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql="SELECT * FROM user where Head_id>1 and City_code=? and Password IS NULL";
		List<User> UserList=jdbcTemplate.query(sql,new Object[] {citycode}, new UserMapper());
		return UserList;
	}
*/
	
}
